package ar.com.larctest.providers;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import ar.com.larctest.dbHelper.SQLLARCTESTHelper;
import ar.com.larctest.entities.HorsesRacing;
import ar.com.larctest.utils.Constants;

public class HorsesRacingProvider {
    private static final String TAG = "HorsesRacingProvider";

	private static final String DB_COLUMN_0_NAME = "idHorseRacing";
	private static final String DB_COLUMN_1_NAME = "idHorse";
    private static final String DB_COLUMN_2_NAME = "dateRacing";
    private static final String DB_COLUMN_3_NAME = "distance";

	private SQLiteDatabase database;
	private SQLLARCTESTHelper dbHelper;
	private String[] allColumns = {DB_COLUMN_0_NAME, DB_COLUMN_1_NAME, DB_COLUMN_2_NAME, DB_COLUMN_3_NAME};

	public HorsesRacingProvider(Context context) {
		dbHelper = SQLLARCTESTHelper.getHelper(context);
		database = dbHelper.getWritableDatabase();
	}
		
	public long insertHorsesRacings(HorsesRacing horsesRacings)
	{
        SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATETIME);

		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_0_NAME, horsesRacings.getIdHorseRacing());
		contentValues.put(DB_COLUMN_1_NAME, horsesRacings.getIdHorse());
        contentValues.put(DB_COLUMN_2_NAME, sdf.format(horsesRacings.getDateRacing()));
        contentValues.put(DB_COLUMN_3_NAME, horsesRacings.getDistance());

        return database.insert(SQLLARCTESTHelper.DB_HORSES_RACING_TABLE_NAME, null, contentValues);
	}
	
	public boolean removeHorsesRacings(Integer idHorseRacings)
	{
		int result = database.delete(SQLLARCTESTHelper.DB_HORSES_RACING_TABLE_NAME, DB_COLUMN_0_NAME + " = " + idHorseRacings, null);

        return result > 0;
	}
	
	public boolean removeRacingsHorse(Integer idHorse)
	{
		int result = database.delete(SQLLARCTESTHelper.DB_HORSES_RACING_TABLE_NAME, DB_COLUMN_1_NAME + " = " + idHorse, null);

        return result > 0;
	}
		
	public long updateHorsesRacings(HorsesRacing horsesRacings)
	{
        SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATETIME);

		ContentValues contentValues = new ContentValues();
        contentValues.put(DB_COLUMN_0_NAME, horsesRacings.getIdHorseRacing());
        contentValues.put(DB_COLUMN_1_NAME, horsesRacings.getIdHorse());
        contentValues.put(DB_COLUMN_2_NAME, sdf.format(horsesRacings.getDateRacing()));
        contentValues.put(DB_COLUMN_3_NAME, horsesRacings.getDistance());

		return database.update(SQLLARCTESTHelper.DB_HORSES_RACING_TABLE_NAME, contentValues, DB_COLUMN_0_NAME + " = " + horsesRacings.getIdHorseRacing(), null);
	}
	
	public HorsesRacing getHorseRacing(Integer idHorseRacing)
	{
		Cursor cursor = null;
        HorsesRacing horsesRacing  = null;
		
		try
		{
            SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATETIME);

            cursor = database.query(SQLLARCTESTHelper.DB_HORSES_RACING_TABLE_NAME, allColumns, DB_COLUMN_0_NAME + " = " + idHorseRacing, null, null, null, null);
			cursor.moveToFirst();

            horsesRacing = new HorsesRacing();
            horsesRacing.setIdHorseRacing(cursor.getInt(0));
            horsesRacing.setIdHorse(cursor.getInt(1)) ;
            horsesRacing.setDateRacing(sdf.parse(cursor.getString(2)));
            horsesRacing.setDistance(cursor.getDouble(3));

			return horsesRacing;
		}
		catch (Exception e) {
			Log.e(TAG, e.getMessage());
			return horsesRacing;
		}
		finally {
            if (cursor != null) {
                cursor.close();
            }
        }
	}
	
	public List<HorsesRacing> getHorsesRacing(Integer idHorse) {
		
		Cursor cursor = null;
		List<HorsesRacing> lstHorses = new ArrayList<HorsesRacing>();
		
		try
		{
            SimpleDateFormat sdf = new SimpleDateFormat(Constants.FORMAT_DATETIME);

            cursor = database.query(SQLLARCTESTHelper.DB_HORSES_RACING_TABLE_NAME, allColumns, DB_COLUMN_1_NAME + " = " + idHorse, null, null, null, null);
			cursor.moveToFirst();
			
			while (!cursor.isAfterLast()) {
                HorsesRacing horsesRacing = new HorsesRacing();
                horsesRacing.setIdHorseRacing(cursor.getInt(0)); ;
                horsesRacing.setIdHorse(cursor.getInt(1)) ;
                horsesRacing.setDateRacing(sdf.parse(cursor.getString(2)));
                horsesRacing.setDistance(cursor.getDouble(3));

                lstHorses.add(horsesRacing);
				cursor.moveToNext();
			}
			
			return lstHorses;
		}
		catch(Exception e) {
			Log.e(TAG, e.getMessage());
			return lstHorses;
		}
		finally {
            if (cursor != null) {
                cursor.close();
            }
        }
	}
}
